<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\OpenAppModel;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\service\cache\CommonCacheKeyService;
use share\service\RedisService;

class OpenAppService extends BaseModelService
{

    CONST STATE_OF_OK = 1;

    CONST STATE_OF_FAIL = 2;

    public static function getModel()
    {
        return new OpenAppModel;
    }

    /**
     * 编辑条目 新增或者修改
     */
    public static function edit($info)
    {
        $id = $info['id'];
        unset($info['id']);
        if ($id == 0) {
            self::addOpenApp($info);
        } else {
            self::updateOpenApp($id, $info);
        }
        //TODO add code here after data change
        return $info;
    }

    /**
     * 新增条目
     */
    public static function addOpenApp($info)
    {
        $info['app_id'] = "open_".date("ymd").mt_rand(10000,99999);
        $info['app_secret'] = "";
        self::delCacheByAppId($info['app_id']);
        self::add($info);
    }

    /**
     * 修改条目
     */
    public static function updateOpenApp($id, $info)
    {
        $origin  = self::getBase($id);
        if (empty($origin)) {
            throw new ApiException("origin data is not exists", ErrnoPlus::COMMON_INVALID_INPUT);
        }
        if(isset($info['app_id'])){
            unset($info['app_id']);
        }
        if(isset($info['app_secret'])){
            unset($info['app_secret']);
        }
        self::delCacheByAppId($info['app_id']);
        self::update($id, $info);
    }

    /**
     * 重置密钥  / 默认恢复异常状态
     */
    public static function resetSecret($id){
        $origin  = self::getBase($id);
        if (empty($origin)) {
            throw new ApiException("reset data is not exists", ErrnoPlus::COMMON_INVALID_INPUT);
        }
        $secret = md5($origin['app_id'].date("YmdHis"));
        $info = [
            'state' => 1,
            'app_secret' => $secret,
        ];
        self::update($id, $info);
        self::delCacheByAppId($origin['app_id']);
        return [
            'id' => $id,
            'app_id' => $origin['app_id'],
            'app_secret' => $secret,
        ];
    }

    /**
     * 删除条目
     */
    public static function delById($id)
    {
        $origin = self::getBase($id);
        if (empty($origin)) {
            return;
        }        
        $res = self::del($id);
        self::delCacheByAppId($origin['app_id']);
        return $res;
    }

    public static function delCacheByAppId($appId){
        $appId = strtolower($appId);
        if(! preg_match("/[a-z0-9\-\_]{10,30}/",$appId)){
            return null;
        }
        $cacheKey = CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_OPEN_APP_ID,['appId' => $appId]);
        RedisService::instance(RedisService::CACHE_DB_BASE)->del($cacheKey);
    }

    /**
     * 
     */
    public static function fastGetByAppId($appId){
        $appId = strtolower($appId);
        if(! preg_match("/[a-z0-9\-\_]{10,30}/",$appId)){
            return null;
        }
        $cacheKey = CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_OPEN_APP_ID,['appId' => $appId]);
        $data = RedisService::instance(RedisService::CACHE_DB_BASE)->getJson($cacheKey);
        if(empty($data)){
            $data = self::getByFilter(['app_id' => $appId]);
            if($data){
                RedisService::instance(RedisService::CACHE_DB_BASE)->setJson($cacheKey,$data,86400);
            }            
        }
        return $data;
    }

    /**
     * 页面查询条件
     */
    public static function buildCondition($filter)
    {
        $res = [];
        if (isset($filter['name']) && !empty($filter['name'])) {
            $res['name'] = ['like', '%' . $filter['name'] . '%'];
        }
        if (isset($filter['app_id']) && !empty($filter['app_id'])) {
            $res['app_id'] = ['like', '%' . $filter['app_id'] . '%'];
        }
        if (isset($filter['is_inner']) && $filter['is_inner'] != "") {
            $res['is_inner'] = intval($filter['is_inner']);
        }
        if (isset($filter['state']) && $filter['state'] != "") {
            $res['state'] = intval($filter['state']);
        }
        return $res;
    }
}
